package com.jgdt.demo.core;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.librariy.util.SystemDisplay;
import com.librariy.util.ViewBinder;
/**基础Fragment*/
public abstract class MultiFragment extends Fragment{
    protected final SystemDisplay sd = SystemDisplay.getInstance(null);
    protected boolean DEBUG=true;
    protected ViewBinder mViewBinder;
    protected boolean isCached = true;
    public void startNewFragment(MultiFragment fragment, Bundle pare, boolean isBackStack) {
        if (getActivity() instanceof MultiActivity) {
            ((MultiActivity) getActivity()).startNewFragment(fragment, pare, isBackStack);
        }
    }
    public void finish() {
        getActivity().onBackPressed();
    }
    /**
     * 指示当前Fragment是否需要拦截【返回】事件
     * @return true-拦截；false-不拦截
     */
    public boolean onBackPressed() {
        return false;
    }
    public abstract View onCreateView(LayoutInflater inflater);
    protected void IntialComponent(Bundle savedInstanceState) {}
    public void setCache(boolean isCached) {
        this.isCached=isCached;
    }
    @Override
    @Deprecated
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (!(getActivity() instanceof MultiActivity)) {
            throw new RuntimeException("[MultiFragment] must be displayed in [MultiActivity] ! ");
        }
        if(isCached==false||mViewBinder==null){
            this.mViewBinder=new ViewBinder(onCreateView(inflater));
            this.IntialComponent(savedInstanceState);
        }
        View v=mViewBinder.getRootView();
        if(v.getParent() instanceof ViewGroup){
            ((ViewGroup)v.getParent()).removeView(v);
        }
        return v;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    @Override
    public void onResume() {
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
    }
}
